package com.haohan.stock.controller;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.haohan.stock.domain.StockBase;
import com.haohan.stock.service.StockBaseService;
import com.haohan.stock.vo.ReturnType;
import com.haohan.util.POIUtil;

@RestController
@RequestMapping(value = "/")
public class UploadDataRestController {
	
	@Autowired
	private StockBaseService stockBaseService;
	
	@RequestMapping(value="/upload",method =RequestMethod.POST)
	public ResponseEntity<ReturnType<String>> get(MultipartFile file) throws Exception {
		ReturnType<String> basedocReturnType = new ReturnType<String>();
		basedocReturnType.setFlag(0);
		basedocReturnType.setDesc("SUCCESS!");
		basedocReturnType.setData("");
		List<String[]> list = POIUtil.readExcel(file);
		String day = file.getOriginalFilename().replace(".xls", "");
		for (Iterator<String[]> iterator = list.iterator(); iterator.hasNext();) {
				String[] strings = (String[]) iterator.next();
				if(strings[2].indexOf("--")!=-1){
					continue;
				}
				StockBase stockBase = new StockBase();
				stockBase.setDay(day);
				stockBase.setCode(strings[1]);
				stockBase.setName(strings[2]);
				stockBase.setModern_price(changeDoubleValue(strings[3]));
				stockBase.setIncrease(changeDoubleValue(strings[4]));
				stockBase.setIncrease_price(changeDoubleValue(strings[5]));
				stockBase.setSum_hands(changeDoubleValue(strings[6]));
				stockBase.setModern_hands(changeDoubleValue(strings[7]));
				stockBase.setBuy_price(changeDoubleValue(strings[8]));
				stockBase.setSell_price(changeDoubleValue(strings[9]));
				stockBase.setIncrease_speed(changeDoubleValue(strings[10]));
				stockBase.setChange_hands(changeDoubleValue(strings[11]));
				stockBase.setMoney(changeDoubleValue(strings[12]));
				stockBase.setPe(changeDoubleValue(strings[13]));
				stockBase.setInsutry(strings[14]);
				stockBase.setMax_price(changeDoubleValue(strings[15]));
				stockBase.setMin_price(changeDoubleValue(strings[16]));
				stockBase.setBegin_price(changeDoubleValue(strings[17]));
				stockBase.setLast_price(changeDoubleValue(strings[18]));
				stockBase.setAmplitude(changeDoubleValue(strings[19]));
				stockBase.setVol_to(changeDoubleValue(strings[20]));
				stockBase.setCommittee(changeDoubleValue(strings[21]));
				stockBase.setCommission(changeDoubleValue(strings[22]));
				stockBase.setAvg_price(changeDoubleValue(strings[23]));
				stockBase.setInside_dish(changeDoubleValue(strings[24]));
				stockBase.setOutside_dish(changeDoubleValue(strings[25]));
				stockBase.setInternal_to_external_ratio(changeDoubleValue(strings[26]));
				stockBase.setBuy_first_vol(changeDoubleValue(strings[27]));
				stockBase.setSell_first_vol(changeDoubleValue(strings[28]));
				stockBase.setPb(changeDoubleValue(strings[29]));
				stockBase.setTotal_capital(changeDoubleValue(strings[30]));
				stockBase.setTotal_marketvalue(changeDoubleValue(strings[31]));
				stockBase.setCirculation_capital(changeDoubleValue(strings[32]));
				stockBase.setCirculation_marketvalue(changeDoubleValue(strings[33]));
				stockBase.setIncrease_3day(changeDoubleValue(strings[34]));
				stockBase.setIncrease_6day(changeDoubleValue(strings[35]));
				stockBase.setChange_3day_hands(changeDoubleValue(strings[36]));
				stockBase.setChange_6day_hands(changeDoubleValue(strings[37]));
				stockBaseService.save(stockBase);
		}
		return new ResponseEntity<ReturnType<String>>(basedocReturnType,HttpStatus.OK);
	}
	
	private double changeDoubleValue(String value){
		if(value.equals("----")){
			return 0d;
		}
		Double valued = 0d;
		if(value.indexOf("万亿")!=-1){
			valued = Double.parseDouble(value.replace("万亿", "").replace("-", ""))*10000d*100000000d;
		}else if(value.indexOf("亿")!=-1){
			valued = Double.parseDouble(value.replace("亿", "").replace("-", ""))*100000000d;
		}else if(value.indexOf("万")!=-1){
			valued = Double.parseDouble(value.replace("万", "").replace("-", ""))*10000d;
		}else{
			valued = Double.parseDouble(value.replace("-", ""));
		}
		if(value.indexOf("-")!=-1){
			valued = 0d-valued;
		}
		return valued;
	} 

}